% The main use of this function is to Compute Profit Y by wwb June 6th

function Y = profit(Q,R,r,ci,cI,I)
Income = 0; Loss = 0;

% There are two parts of Y.
% Firstly, we compute the income.
for i = 1:I
    length = size(Q, 2);
    for m = 1:length
        Income = Income + r * (m-1) *Q(i,m);
    end
end

% Secondly, we compute Losses.
len = size(R, 2);
for i = 1:(I-1)
    for mm = 1:len
        Loss = Loss + ci * (mm-1) *R(i,mm);
    end
end

for mm = 1:len
    Loss = Loss + cI * (mm-1) * R(I,mm);
end

% Finally we get the profit!
Y = Income - Loss;
end